package pe.edu.upla.lamerced.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import org.springframework.beans.factory.annotation.Autowired;

import pe.edu.upla.lamerced.bean.Habitacion;
import pe.edu.upla.lamerced.bean.Hospedaje;
import pe.edu.upla.lamerced.bean.Users;
import pe.edu.upla.lamerced.service.HabitacionService;
import pe.edu.upla.lamerced.service.HospedajeService;
import pe.edu.upla.lamerced.service.UsuarioService;
import pe.edu.upla.lamerced.utiles.Utiles;

public class ReservasAction extends DispatchAction {

	private static final Logger logger = Logger.getLogger(ReservasAction.class);
	
	@Autowired
	HabitacionService habitacionService;
	@Autowired
	HospedajeService hospedajeService;
	@Autowired
	UsuarioService usuarioService;
	
	public ActionForward preReserva(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)	throws Exception {
		try {
			
			request.setAttribute("lstipohabitacion", habitacionService.listaTipoHabitacion());
			request.setAttribute("lsHabitacion", new ArrayList<Habitacion>());
			
            logger.debug("en el metodo pre reserva");
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msgError", e.getMessage());
            return mapping.findForward("home");
		}
		return mapping.findForward("reserva");
	}
	public ActionForward reserva(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)	throws Exception {
		try {
			DynaActionForm miform = (DynaActionForm)form;
			Hospedaje hos = new Hospedaje();
			hos.setFecReserva(Utiles.stringToDate(miform.getString("fecReserva"), Utiles.FORMATO_FECHA_CORTA));
			hos.setDiasreserva(Integer.parseInt(miform.getString("diasreserva")));
			Habitacion hab = new Habitacion();
			hab.setIdhabitacion(Integer.parseInt(miform.getString("idhabitacion")));
			hos.setHabitacion(hab);
			hos.setEstado(1);
			Users ulogin = Users.getUsuarioBean();
			if(ulogin.getTipo()==1){
				hos.setUsersByIdUsuarioCli(usuarioService.obtenerUsuarioPorUsername(request.getParameter("cliente")));
				hos.setUsersByIdUsuarioEmp(ulogin);
			}
			if(ulogin.getTipo()==2){
				hos.setUsersByIdUsuarioCli(ulogin);
				hos.setUsersByIdUsuarioEmp(usuarioService.obtenerUsuarioPorUsername("999999999"));	
			}
			hospedajeService.registrarReserva(hos);	
			hos = hospedajeService.obtenerReserva(hos.getIdhospedaje());
            logger.debug(" RESERVA RALIZADA");
            request.setAttribute("mensaje", "Reserva registrada");
            request.setAttribute("rdet", hos);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msgError", e.getMessage());
            request.setAttribute("lstipohabitacion", habitacionService.listaTipoHabitacion());
			request.setAttribute("lsHabitacion", new ArrayList<Habitacion>());
            return mapping.findForward("reserva");
		}
		return mapping.findForward("confirmar");
	}
	
	public ActionForward consultaReservas(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)	throws Exception {
		try {
			//TODO:validar tipo de usuario
			Users ulogin = Users.getUsuarioBean();
			Hospedaje ho = new Hospedaje();			
			ho.setUsersByIdUsuarioCli(null);
			Habitacion hab = new Habitacion();
			if(ulogin.getTipo()==1){
				
			}
			if(ulogin.getTipo()==2){
				ho.setUsersByIdUsuarioCli(ulogin);
			}
			//si el usuario logueado es empleado de la empresa, muestra todas las reservas			
			hab.setEstado(1);
			ho.setHabitacion(hab);			
			ho.setEstado(1);
			request.setAttribute("lreservas", hospedajeService.listarRserva(ho));
			
            logger.debug("CONSULTA RESERVAS DE CLIENTE SESSION");
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msgError", e.getMessage());
            return mapping.findForward("home");
		}
		return mapping.findForward("consulta");
	}
	
	public ActionForward anulaReserva(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)	throws Exception {
		try {
            logger.debug(" anular reserva ");
            String hospedaje = request.getParameter("cod");
            hospedajeService.anularReserva(Integer.parseInt(hospedaje));            
		} catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msgError", e.getMessage());
            return mapping.findForward("procesosHome");
		}
		//return mapping.findForward("consulta");
		return consultaReservas(mapping, form, request, response);
	}
	
}